<?php
$this->pageTitle = Yii::t('view', 'Peta');
$this->breadcrumbs=array(
	Yii::t('view', 'Peta'),
);
    Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl . "/assets/css/leaflet.css");
    Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . "/assets/js/leaflet.js", CClientScript::POS_END);
    Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . "/files/map/indonesia.js", CClientScript::POS_END);
?>
<div class="container">
    <div class="row">
        <div class="col-md-2">
            <div class="col-md-12 mini-bot-pad">
                <a href="<?php echo Yii::app()->createUrl('/site/map/1')?>">
                    <img src="<?php echo Yii::app()->theme->baseUrl; ?>/assets/images/pmb.png" class="img-responsive">
                </a>
            </div>
           <div class="col-md-12 mini-bot-pad">
                <a href="<?php echo Yii::app()->createUrl('/site/map/2')?>">
                    <img src="<?php echo Yii::app()->theme->baseUrl; ?>/assets/images/pgb.png" class="img-responsive">
                </a>
            </div>
            <div class="col-md-12 mini-bot-pad">
                <a href="<?php echo Yii::app()->createUrl('/site/map/3')?>">
                    <img src="<?php echo Yii::app()->theme->baseUrl; ?>/assets/images/pho.png" class="img-responsive">
                </a>
            </div>
            <div class="col-md-12 mini-bot-pad btn-peta-tab">
                <a class="btn btn-lg btn-default" href="<?php echo Yii::app()->createUrl('/site/map')?>">
                    <?php echo Yii::t('view', 'Semua Data')?>
                </a>
            </div>
            <div class="col-md-12 mini-bot-pad btn-peta-tab">
                <a class="btn btn-lg btn-default" href="<?php echo Yii::app()->createUrl('/site/tabular')?>">
                    <?php echo Yii::t('view', 'Data Tabular')?>
                </a>
            </div>
        </div>
        <div class="col-md-10">  
                <div class="map-container">
                    <div id="map" style="min-height:450px;height:100%;"></div>
                    <div class="alert alert-info" role="alert"><?php echo Yii::t('view','*) Klik icon untuk mengetahui daftar Badan Usaha berdasarkan Provinsi.');?></div>
                </div> 
        </div>
    </div>
</div>
<?php
$marker = '';
if ($data && !empty($data)) {
    foreach ($data as $v) {
        $rand = md5(mt_srand());
        $koord = explode(',', $v->provinsi->koordinat);
        $izin = $this->getPerusahaan($v->provinsi_id);
        $icon = $iconType;
        $content = ""
                . "<div class='panel panel-default col-md-13'>"
                . "<div class='panel-heading'><h3 class='panel-title'>Provinsi " . $v->provinsi->nama . "</h3></div>"
                . "<div class='panel-body'>"
                . "<table>"
                . "<tr><th>No</th><th>Badan Usaha</th><th>Lokasi</th><th>Kapasitas Pengolahan</th></tr>";
                $i=1;
                foreach ($izin as $v) {
                   $bu = ($v->perusahaan->bentuk_badan_usaha) ? $v->perusahaan->bentuk_badan_usaha : '';
                   $lokasi = ($v->lokasi) ? $v->lokasi : '-';
                   $kapasitas = ($v->kapasitas_pengolahan) ? $v->kapasitas_pengolahan : '-';
                   $content .= "<tr><td>".$i."</td><td>".$bu.". ".$v->perusahaan->nama_perusahaan."</td><td>".$lokasi."</td><td>".$kapasitas."</td></tr>";
                    $i++;
                }
                if($i===4){
                    $content .= "<tr><td></td><td></td><td></td><td><a href='".Yii::app()->createUrl('/site/mapdetail', array('id'=>  strtolower($v->provinsi_id)))."'>Lainnya...</a></td></tr>";
                }
                $content .= "</table>"
                        ."</div>"
                        . "</div>";
        $marker .= "var $rand = L.marker([" . $koord[0] . ", " . $koord[1] . "], {icon: $icon}).addTo(map);\n";
        $marker .= $rand . ".bindPopup(\"$content\");\n";
    }
}
    $map_code = "
function getColor(e) {
    var d = e.toString().substr(0, 1);
    return '#696B97';
//    return d == 1 ? '#800026' :
//           d == 2  ? '#BD0026' :
//           d == 3  ? '#E31A1C' :
//           d == 4  ? '#FC4E2A' :
//           d == 5   ? '#FD8D3C' :
//           d == 6   ? '#FEB24C' :
//           d == 7   ? '#FECC12' :
//           d == 8   ? '#FED976' :
//                      '#FFEDA0';
}
window.onload = function () {
        var map = L.map('map', {
            center: [-2.5920, 118.42654],
            zoom: 4.8,
            zoomControl: true
        });
		//var icon_0 = L.icon({iconUrl: '/files/icon/marker_not_set.png',iconSize:[21,41],iconAnchor:[12,41],popupAnchor:[1,-34],shadowSize:[41,41]});
		var icon_1 = L.icon({iconUrl: '".Yii::app()->baseUrl."/files/icon/marker_complete.png',iconSize:[21,41],iconAnchor:[12,41],popupAnchor:[1,-34],shadowSize:[41,41]});
		var icon_2 = L.icon({iconUrl: '".Yii::app()->baseUrl."/files/icon/marker_on_schedule.png',iconSize:[21,41],iconAnchor:[12,41],popupAnchor:[1,-34],shadowSize:[41,41]});
		var icon_3 = L.icon({iconUrl: '".Yii::app()->baseUrl."/files/icon/marker_behind_schedule.png',iconSize:[21,41],iconAnchor:[12,41],popupAnchor:[1,-34],shadowSize:[41,41]});
		var icon_4 = L.icon({iconUrl: '".Yii::app()->baseUrl."/files/icon/marker_not_yet.png',iconSize:[21,41],iconAnchor:[12,41],popupAnchor:[1,-34],shadowSize:[41,41]});
        //map.dragging.disable();
        map.touchZoom.disable();
        map.doubleClickZoom.disable();
        map.scrollWheelZoom.disable();
        if (map.tap) map.tap.disable();        
        function highlightFeature(e) {
            var layer = e.target;
            var feature = layer.feature.properties;
            if (feature && feature.PROPINSI_) {
                layer.bindPopup('Provinsi: '+feature.PROPINSI_+'<br><br>'+e.latlng).openPopup();
            }
        }
        function onEachFeature(feature, layer) {
            layer.on({
                click: highlightFeature
            });            
        }        
        function style(feature) {
            return {
                fillColor: getColor(feature.properties.KODE),
                weight: 2,
                opacity: 1,
                color: 'white',
                dashArray: '3',
                fillOpacity: 0.7
            };
        }
	var countryStyle = {
		'color': '#000',
		'weight': 2,
		'opacity': 0.6
	};
" . $marker . "
	L.geoJson(Indo, {
		style: style,
                onEachFeature: onEachFeature
	}).addTo(map);
};
";
    Yii::app()->clientScript->registerScript('leaflet', $map_code);
?>
